import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template


from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
   def get(self):
     template_values = {}
     path = os.path.join(os.path.dirname(__file__), 'hello.html')
     self.response.out.write(template.render(path, template_values))

class goodbyeHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('LALLALALA')

class diceHandler(webapp.RequestHandler):
  def get(self):
    times = self.request.get('times')
    #! /usr/bin/python
    # A Python script that simulates rolling two dice a few times.

    # Module for dealing with random numbers.
    import random


    # Number of times to roll the dice.
    DICE_ROLLS = 'many'
    roll_num = 0

    self.response.out.write("Rolling the dice " + DICE_ROLLS + " times <br> ")

    while (roll_num < int(times)):
        # Simulate rolling two dice.
        roll_num += 1
        dice_1 = random.randint(1, 6)
        dice_2 = random.randint(1, 6)
        self.response.out.write("Rolls: " + str(dice_1) + " " + str(dice_2) + "<br>")
        if dice_1 == 1 and dice_2 == 1:
            print "Got Snake Eyes"
        if dice_1 == 6 and dice_2 == 6:
	    print "Got Double Sixes"
	    break

    self.response.out.write("The Dice was rolled " + str(roll_num) + " Times <br>" + "Woo Hoo!")

class QuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = {'name' : self.request.get('name')}
    path = os.path.join(os.path.dirname(__file__), 'quiz.html')
    self.response.out.write(template.render(path, template_values))

class WrongAnswer:
  def __init__(self, question, guess, correct):
    self.question = question
    self.guess = guess
    self.correct = correct

class GradeQuizHandler(webapp.RequestHandler):
  def get(self):
    addition_guess = self.request.get('addition')
    template_values = {'name':self.request.get('name'),
		       'wrong_answers': []}
    capital_guess = self.request.get('capital')
    if addition_guess != "7":
      template_values['wrong_answers'].append(WrongAnswer("3 + 4", addition_guess, 7))
    if capital_guess != "Sacramento":
      template_values['wrong_answers'].append(WrongAnswer("\"What is the capital of California?\"", capital_guess, "Sacramento"))
    path = os.path.join(os.path.dirname(__file__), 'grade_quiz.html')
    self.response.out.write(template.render(path, template_values))


def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler),
		                        ('/goodbye.*', goodbyeHandler),
					('/dice.*', diceHandler),
					('/quiz', QuizHandler),
					('/grade_quiz', GradeQuizHandler),], debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
